/**
 * 随机创建测试用数据，条数不会超过姓名排列组合的85%与5000之间的最小值，返回内容按retType
 * @param {Number} num 创建条数，默认为50条，超过5000条则只生成5000条
 * @param {Number} retType 默认为全记录（嵌套JSON形式的数据），1-员工信息部分，2-职务字典，3-部门字典，4-性别字典
 */

function dataMaker(num, retType) {
	var num = num || 50;
	var _fName = ["陈", "李", "张", "黄", "何", "史", "廖", "林", "白", "蔡", "王", "杜", "朱", "刘", "唐", "韦", "胡", "莫", "戴"]; //15
	var _mName = ["子", "建", "小", "俊", "嘉", "志", "泽", "文", "少", "元", "思"]; //11
	var _lName = ["昌", "钧", "东", "珍", "民", "伟", "威", "浩", "莹", "明", "佳", "军", "娟", "国", "敏", "雄", "杰", "珊", "荣", "光", "英", "坤", "锋", "健", "华", "勇", "妍", "红", "芳", "聪", "晶", "鑫", "淼", "磊", "品", "森", "众"]; //37
	var _jobs = ["技术员", "技术经理", "高级技术经理", "架构师"];
	var _jobsList = [];
	for (var i = 0; i < _jobs.length; i++) {
		_jobsList[i] = {
			text: _jobs[i],
			value: i+""
		};
	};
	var _depts = ["开发一部", "开发二部", "开发三部", "开发四部", "系统部", "项目办"];
	var _deptsList = [];
	for (var i = 0; i < _depts.length; i++) {
		_deptsList[i] = {
			text: _depts[i],
			value: i+""
		};
	};
	var _sexList = [{
		text: "男",
		value: "0"
	}, {
		text: "女",
		value: "1"
	}];
	var _jobsCount = [0, 0, 0, 0]
	var _jobsMax = [num, parseInt(num / 10), parseInt(num / 20), parseInt(num / 50)]; //设定职务比例
	var _female = ["珍", "莹", "娟", "珊", "敏", "英", "妍", "红", "芳", "晶"];
	//最大条数限制，大于总量80%后可能会难以命中
	var _maxNum = Math.max(((_fName.length * _lName.length) + (_fName.length * _mName.length * _lName.length)) * 0.8,5000);
	if (num > _maxNum) {
		num = _maxNum;
	}
	flag = true
	var i = 0;
	var names = [],
		_f, _m, _l, _name, rec = {},
		ret = [];
	while (flag && (!retType || retType==1)) {
		rec = {};
		_f = Math.floor(Math.random() * (_fName.length), _fName.length - 1);
		_m = parseInt(Math.random() * 15); //姓名中间字允许为空（即姓名可以只有两字）
		_l = Math.floor(Math.random() * (_lName.length), _fName.length - 1);
		_j = Math.floor(Math.random() * (_jobs.length), _jobs.length - 1);
		_d = Math.floor(Math.random() * (_depts.length), _depts.length - 1);
		_job = _jobsList[_j].value;
		_jobsCount[_j]++;
		_name = _fName[_f] + (_mName[_m] || "") + _lName[_l];
		if (_jobsCount[_j] > _jobsMax[_j]) {
			_job = 0; //超过职务比例限制时
		}
		if (names.join(",").indexOf(_name) == -1) {
			i++;
			names.push(_name);
			rec.name = _name;
			rec.sex = _female.join(",").indexOf(_lName[_l]) > -1 ? "1" : "0"; //按名字设置男女
			rec.salary = (parseInt((Math.random() * 10000 + 3000) * 100) / 100)+"";
			rec.ID = "ID_" + ("00000" + i.toString()).substring(("00000" + i.toString()).length - num.toString().length); //自动生成ID
			rec.job = _job+"";
			rec.dept = _deptsList[_d].value+"";
			ret.push(rec);
		};
		if (names.length == num) {
			flag = false;
		}
	}

	var rs;
	switch (retType) {
	case 1:
		rs = ret;
		break;
	case 2:
		rs = {
			jobs: _jobsList
		};
		break;
	case 3:
		rs = {
			depts: _deptsList
		};
		break;
	case 4:
		rs = {
			sex: _sexList
		};
		break;
	default:
		rs = [{
			info: {
				totalNum: names.length,
				data: ret
			},
			jobs: _jobsList,
			depts: _deptsList,
			sex: _sexList
		}];
		break;
	}
	return rs;
}